var app = angular.module('ZJPC');
app.controller('FellowPeopleController', ['$scope','$state','$rootScope','$stateParams','CesiumService','DataService','$filter',FellowPeopleController]);

function FellowPeopleController($scope,$state,$rootScope,$stateParams,CesiumService,DataService,$filter) {

	$scope.$on('detaildata',function(e,data){
		$scope.fpeople = data.fellowper;
		$scope.starttime = data.starttime;
		$scope.trackName = data.xm;
		$scope.trackGroup = data.trackId;
	})

	if($stateParams.datag!=null){
		$scope.fpeople = $stateParams.datag.fellowper;
		$scope.starttime = $stateParams.datag.starttime;
		$scope.trackName = $stateParams.datag.xm;
		$scope.trackGroup = $stateParams.datag.trackId;
	}

	$scope.chosen = 0;
	var starttime = new Date($scope.starttime).setHours(0,0,0)
	var endtime = new Date($scope.starttime).setHours(23,59,59)

	if($scope.fpeople!=null&&$scope.fpeople.length!=0){

		searchTrack(0)
/*		DataService.getPerTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.fpeople[0].xhgh,function(data){
			var dataArray = []
			console.log(data)
			if(data.length>0){
				for(var i =0;i<data.length;i++){
					dataArray.push({time:data[i].passTime,track:[data[i].camerJd,data[i].camerWd]})
				}
				if($stateParams.data.gh!=undefined){
					var idGroup = CesiumService.getTrack(dataArray, dataArray,'fellow','teacher')
				}else if($stateParams.data.xh!=undefined){
					var idGroup = CesiumService.getTrack(dataArray, dataArray,'fellow','student')
				}else{
					var idGroup = CesiumService.getTrack(dataArray, dataArray, 'fellow','outer')
				}
				$scope.fellow_track = idGroup[0]
				$scope.fellow_point = idGroup[1]
			}
		})*/	
	}


	//点选控制
	$scope.check = function(index){
		if($scope.chosen != index){
			$scope.chosen = index;
			searchTrack(index)
			
		}
	}

	$scope.chosenTrack = 0;

	$scope.checkTrack = function(i){
		$scope.chosenTrack = i;
		//0的情况,代表选中本轨迹
		if(i==0){
			CesiumService.removeTrack($scope.fellow_point)
			CesiumService.showTrack($scope.trackGroup[1])
			//hide同行轨迹点
			//show本轨迹
		}else{
			CesiumService.showTrack($scope.fellow_point)
			CesiumService.removeTrack($scope.trackGroup[1])
			//show同行轨迹点
			//hide本轨迹点
		}
	}
/*
	$scope.$on('starttime',function(e,d){
		$scope.starttime = d;
		var starttime = new Date($scope.starttime).setHours(0,0,0)
		var endtime = new Date($scope.starttime).setHours(23,59,59)
		DataServce.getPerTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.fpeople[newValue].xhgh,function(data)){
			CesiumService.getTrack('fellow')
		}		

	})*/

	function searchTrack(index){
		$scope.loading = true;	
		DataService.getPerTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.fpeople[index].jid,function(data){
			var dataArray = []
			$scope.loading = false;
			if(data.track.length>0){
				var ways =  data.ways[0].ways.split(';');
				for(var i =0;i<ways.length-1;i++){
					dataArray.push({track:ways[i].split(',')});
				}
				if($scope.fellow_track!=undefined){
					CesiumService.removeTrack($scope.fellow_track)		
				}
				if($scope.fellow_track!=undefined){
					CesiumService.removeTrack($scope.fellow_point)
				}
				//简单判断
				if($scope.fpeople[index].jid.length==6){
					var group = CesiumService.getTrack(data.ways[0].camer1Time,data.ways[0].camer2Time,dataArray,data.track,'fellow','student')
				}else if($scope.fpeople[index].jid.length==7){
					var group = CesiumService.getTrack(data.ways[0].camer1Time,data.ways[0].camer2Time,dataArray,data.track,'fellow','teacher')
				}else{
					var group = CesiumService.getTrack(data.ways[0].camer1Time,data.ways[0].camer2Time,dataArray,data.track,'fellow','outer')
				}
				$scope.fellow_track = group[0]
				$scope.fellow_point = group[1]

			}
		})	
	}

   $scope.$on("$destroy", function() {
        CesiumService.removeTrack($scope.fellow_track)
        CesiumService.removeTrack($scope.fellow_point)		
    })

/*	$scope.$watch('chosen',function(newValue,oldValue){
		console.log(1)
		if(newValue!=oldValue){
			searchTrack(newValue)
		}
	})*/

}